import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
// import NavBar from '@/components/NavBar'
import paper from '@/components/paper'
import editor from '@/components/editor'
import HomeList from '@/components/HomeList'
import login from '@/components/Login'
import cM from '@/components/convertMoney'
import tools from '@/components/tools'
import pwd from '@/components/pwd'

Vue.use(Router)
//
// export default new Router({
//   routes: [
//     {
//       path: '/',
//       name: 'HelloWorld',
//       component: HelloWorld
//     },
//     {
//       path: '/HomeList',
//       name: '首页',
//       component: HomeList
//     },
//     {
//       path: '/e',
//       name: 'editor',
//       component: editor
//     },
//     {
//       path: '/p/:id',
//       name: '正文',
//       component: paper
//     },
//     {
//       path: '/l/',
//       name: 'login',
//       component: login
//     }
//   ]
// })

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      // name: 'HelloWorld',
      // component: HelloWorld
      name: '首页2',
      component: HomeList,
      meta: {
        title: '南星'
      }
    },
    {
      path: '/HomeList',
      name: '首页',
      component: HomeList,
      meta: {
        title: '南星'
      }
    },
    {
      path: '/e',
      name: 'editor',
      component: editor,
      meta: {auth: true, title: '南星'} // 设置当前路由需要校验  不需要校验的路由就不用写了
    },
    {
      path: '/p/:id',
      name: '正文',
      component: paper,
      meta: {
      }
    },
    {
      path: '/l/',
      name: 'login',
      component: login,
      meta: {
        title: '登录--南星'
      }
    },
    {
      path: '/cM/',
      name: 'cM',
      component: cM,
      meta: {
        title: '人民币大写在线转换工具'
      }
    },
    {
      path: '/tools/',
      name: 'tools',
      component: tools,
      meta: {
        title: '常用工具'
      }
    },
    { path: '/pwd/',
      name: 'pwd',
      component: pwd,
      meta: {
        title: '密码生成工具'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(from)
  // 设置标题
  document.title = to.meta.title
  if (to.matched.some(m => m.meta.auth)) {
    console.log('need auth!')
    // 对路由进行验证
    if (sessionStorage.userName) { // 已经登陆
      console.log('auth true')
      next() // 正常跳转到你设置好的页面
    } else {
      console.log('auth false')
      // 未登录则跳转到登陆界面，query:{ Rurl: to.fullPath}表示把当前路由信息传递过去方便登录后跳转回来；
      next({path: '/l/', query: {Rurl: to.fullPath}})
    }
  } else {
    next()
  }
})

export default router
